package com.invengo.library_rfid.analyze;

public class ConvertUtil {

    /**
     * 字节转十六进制
     * @param b 需要进行转换的byte字节
     * @return  转换后的Hex字符串
     */
    public static String byteToHex(byte b){
        String hex = Integer.toHexString(b & 0xFF);
        if(hex.length() < 2){
            hex = "0" + hex;
        }
        return hex;
    }


    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return  转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }


    /**
     * Hex字符串转byte
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte
     */
    public static byte hexToByte(String inHex){
        return (byte)Integer.parseInt(inHex,16);
    }


    /**
     * hex字符串转byte数组
     * @param hex16Str 待转换的Hex字符串
     * @return  转换后的byte数组结果
     */
    public static byte[] hexToBytes(String hex16Str){
        char[] c = hex16Str.toCharArray();
        byte[] b = new byte[c.length / 2];
        for (int i = 0; i < b.length; i++) {
            int pos = i * 2;
            b[i] = (byte) ("0123456789ABCDEF".indexOf(c[pos]) << 4 | "0123456789ABCDEF".indexOf(c[pos + 1]));
        }
        return b;
    }

    /**
     * hex字符串转int
     * @param inHex
     * @return
     */
    public static int hexToInt(String inHex){
        return Integer.valueOf(inHex,16);
    }

    /**
     * int到byte[]
     * @param i
     * @return
     */
    public static byte[] intToBytes(int i) {
        byte[] result = new byte[4];
        // 由高位到低位
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    /**
     * byte[]转int
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }

    /**
     * 将字节数组转为long<br>
     * 如果input为null,或offset指定的剩余数组长度不足8字节则抛出异常
     * @param input
     * @param offset 起始偏移量
     * @param littleEndian 输入数组是否小端模式
     * @return
     */
    public static long bytesToLong(byte[] input, int offset, boolean littleEndian){
        long value=0;
        // 循环读取每个字节通过移位运算完成long的8个字节拼装
        for(int  count=0;count<8;++count){
            int shift=(littleEndian?count:(7-count))<<3;
            value |=((long)0xff<< shift) & ((long)input[offset+count] << shift);
        }
        return value;
    }

    public static long bytesToLong(byte[] bytes){
        long value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }

    /**
     * byte数组转二进制字符串
     * @param bytes
     * @return
     */
    public static String bytesToBin(byte[] bytes){
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            result.append(Long.toString(bytes[i] & 0xff, 2));
        }
        return result.toString().substring(0, result.length() - 1);
    }

    /**
     * str16到str2
     *
     * @param string 一串
     * @return {@link String}
     */
    public static String str16ToStr2(String string) {

        if (string == null || string.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < string.length(); i++)
        {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(string
                    .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    public static String str2ToStr16(String string) {
        if (string == null || string.equals("") || string.length() % 4 != 0) {
            return null;
        }

        StringBuffer sbs = new StringBuffer();
        // 二进制字符串是4的倍数，所以四位二进制转换成一位十六进制
        for (int i = 0; i < string.length() / 4; i++) {
            String subStr = string.substring(i * 4, i * 4 + 4);
            String hexStr = Integer.toHexString(Integer.parseInt(subStr, 2));
            sbs.append(hexStr);
        }

        return sbs.toString();
    }

    public static int str2ToInteger(String str2) {
        int decimal = str2.charAt(str2.length() - 1) == '1' ? (int)Math.pow(2, str2.length()-1) : 0;
        return (str2.length() == 1) ? decimal :
                decimal + str2ToInteger(str2.substring(0, str2.length()-1));
    }

}
